package city.spring.modules.monthlyfee.controller;

import city.spring.modules.monthlyfee.entity.MonthlyfeeEntity;
import city.spring.modules.monthlyfee.service.MonthlyfeeService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：月费用
 *
 * @author 谢金婷
 * @date 2020-09-10 16:52:52
 */
@RestController
@RequestMapping("/monthlyfee")
public class MonthlyfeeController extends ApiController {
    private final MonthlyfeeService monthlyfeeService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<MonthlyfeeEntity, ?>> entityOrderFields;

    public MonthlyfeeController(MonthlyfeeService monthlyfeeService) {
        this.monthlyfeeService = monthlyfeeService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(MonthlyfeeEntity::getId);
        entityOrderFields.add(MonthlyfeeEntity::getMonthlyfeeDate);
        entityOrderFields.add(MonthlyfeeEntity::getCostCategory);
        entityOrderFields.add(MonthlyfeeEntity::getPrice);
        entityOrderFields.add(MonthlyfeeEntity::getDescription);
        entityOrderFields.add(MonthlyfeeEntity::getGmtCreate);
        entityOrderFields.add(MonthlyfeeEntity::getGmtModified);
    }

    /**
     * 获取全部的 <strong>月费用</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault Pageable pageable, MonthlyfeeEntity entity) {
        LambdaQueryChainWrapper<MonthlyfeeEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>月费用</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault Pageable pageable, MonthlyfeeEntity entity) {
        LambdaQueryChainWrapper<MonthlyfeeEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<MonthlyfeeEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        return success(page);
    }

    /**
     * 获取一个 <strong>月费用</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(monthlyfeeService.getById(id));
    }

    /**
     * 添加一个 <strong>月费用</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody MonthlyfeeEntity entity) {
        monthlyfeeService.saveMonthlyfee(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>月费用</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody MonthlyfeeEntity entity) {
        entity.setId(id);
        monthlyfeeService.updateMonthlyfee(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>月费用</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        monthlyfeeService.deleteMonthlyfee(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>月费用</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        monthlyfeeService.deleteMonthlyfee(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<MonthlyfeeEntity> buildLambdaQuery(MonthlyfeeEntity entity) {
        LambdaQueryChainWrapper<MonthlyfeeEntity> lambdaQuery = monthlyfeeService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), MonthlyfeeEntity::getId, entity.getId());
        lambdaQuery.eq(entity.getMonthlyfeeDate() != null, MonthlyfeeEntity::getMonthlyfeeDate, entity.getMonthlyfeeDate());
        lambdaQuery.eq(entity.getCostCategory() != null, MonthlyfeeEntity::getCostCategory, entity.getCostCategory());
        lambdaQuery.eq(entity.getPrice() != null, MonthlyfeeEntity::getPrice, entity.getPrice());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getDescription()), MonthlyfeeEntity::getDescription, entity.getDescription());
        lambdaQuery.eq(entity.getGmtCreate() != null, MonthlyfeeEntity::getGmtCreate, entity.getGmtCreate());
        lambdaQuery.eq(entity.getGmtModified() != null, MonthlyfeeEntity::getGmtModified, entity.getGmtModified());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<MonthlyfeeEntity> buildQuery(MonthlyfeeEntity entity) {
        QueryWrapper<MonthlyfeeEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(entity.getId()), "id", entity.getId());
        wrapper.eq(entity.getMonthlyfeeDate() != null, "monthlyfee_date", entity.getMonthlyfeeDate());
        wrapper.eq(entity.getCostCategory() != null, "cost_category", entity.getCostCategory());
        wrapper.eq(entity.getPrice() != null, "price", entity.getPrice());
        wrapper.like(StringUtils.isNotBlank(entity.getDescription()), "description", entity.getDescription());
        wrapper.eq(entity.getGmtCreate() != null, "gmt_create", entity.getGmtCreate());
        wrapper.eq(entity.getGmtModified() != null, "gmt_modified", entity.getGmtModified());
        return wrapper;
    }

}
